package com.tust.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.consts.Constants;
import com.tust.app.domain.News;
import com.tust.app.domain.TUser;
import com.tust.app.service.NewsService;
import com.tust.app.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
public class NewsController {

    @Autowired
    private NewsService newsService;
    private TUserService userService;
    @RequestMapping("/")
    public String home(HttpServletRequest request) {
        return "forward:/page";
    }

    @RequestMapping("/page")
    public String page(HttpServletRequest request){
        try {
            PageHelper.startPage(Constants.mainPageNum, Constants.mainPageSize);
            List<News> news = newsService.newsList();
            PageInfo page = new PageInfo(news);
            request.setAttribute("pageInfo", page);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("按固定页码做分页查询失败！");
        }

        return "home";
    }

    @RequestMapping("/page/{pageNum}")
    public String page2(@PathVariable("pageNum") Integer pageNum, HttpServletRequest request){
        try {
            PageHelper.startPage(pageNum, Constants.mainPageSize);
            List<News> news = newsService.newsList();
            PageInfo page = new PageInfo(news);
            request.setAttribute("pageInfo", page);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("按页码做分页查询失败！");
        }

        return "home";
    }

    /*展示新闻列表*/
    @RequestMapping("/anews")
    public String anews(Integer pn,HttpServletRequest request){
        try {
            if(pn==null){
                pn=1;
            }
            PageHelper.startPage(pn, Constants.mainPageSize);
            List<News> news = newsService.newsList();
            PageInfo page = new PageInfo(news);
            request.setAttribute("news", page);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("展示新闻列表出错！");
        }
        return "admin/news/adminPageList";
    }
    public String anews2(@PathVariable("pn") Integer pageNum, HttpServletRequest request){
        try {
            PageHelper.startPage(Constants.mainPageNum, Constants.mainPageSize);
            List<News> news = newsService.newsList();
            PageInfo page = new PageInfo(news);
            request.setAttribute("news", page);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("分页查询，这个接口没用");
        }
        return "admin/news/adminPageList";
    }
    @RequestMapping("/selectByTitle")
    public String selectByTitle( String title,HttpServletRequest request) {
        try {
            PageHelper.startPage(Constants.titlePageNum,Constants.titlePageSize);
            List<News> searchList=newsService.serach(title);
            PageInfo pageInfo=new PageInfo(searchList);
            request.setAttribute("news",pageInfo);
            if (searchList != null) {
                return "admin/news/adminPageList";
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查询新闻标题条数异常！");
        }

        return "error";
    }
    @RequestMapping("/writeNews")
    public String writeNews(String title,String article,HttpServletRequest request){
        try {
            News news=new News();
            news.setTitle(title);
            news.setArticle(article);
            Date data=new Date();
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            String format1 = format.format(data);
            news.setTime(format1);
            Integer i=newsService.addArticle(news);
            if(i>0){
                PageHelper.startPage(Constants.mainPageNum, 10);
                List<News> news1 = newsService.newsList();
                PageInfo pageInfo = new PageInfo(news1);
                request.setAttribute("news", pageInfo);
                return "admin/news/adminPageList";
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("增加一条新闻异常！！！");
        }

        return "error";
    }

    @RequestMapping("/getNews")
    public String getNews(Integer id,HttpServletRequest request){
        try {
            News news = newsService.findNewsById(id);
            request.setAttribute("news", news);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("根据id查询新闻异常");
        }

        return "admin/news/editNews";
    }

    @RequestMapping("/updateNews")
    public String updateNews(Integer id,String title,String article){
        try{
            //实现修改过程
            News news =new News();
            news.setId(id);
            news.setTitle(title);
            news.setArticle(article);
            boolean b= newsService.modifyNews(news);
            if(b){
                return "forward:/anews";
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("修改新闻异常！");
        }

        return "error";
    }
    @RequestMapping("/deleteNewsById")
    public String deleteNewsByI(Integer id){
        try{
            boolean b= newsService.delNews(id);
            if(b){
                return "forward:/anews";
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("根据id删除新闻异常");
        }

        return "error";
    }


    @RequestMapping("/selectNewsById")
    public  String  selectNewsById(Integer id,HttpServletRequest request){
        try {
            News news=newsService.findNewsById(id);
            request.setAttribute("news",news);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("根据id查询新闻异常！");
        }

        return "detail";
    }

}
